#include "all.h"
// STM32 数据序列化
cJSON *serialize_stm32(struct STM32 *stm32)
{
    cJSON *stm32_data = cJSON_CreateArray();
 
    for (int i = 0; i < 14; i++)
    {
        cJSON *item = cJSON_CreateObject();
        cJSON_AddNumberToObject(item, "key", stm32->data[i].data_key);
        cJSON_AddStringToObject(item, "name", stm32->data[i].data_name);
        cJSON_AddNumberToObject(item, "type", stm32->data[i].data_type);

        // 根据数据类型选择序列化字段
        if (stm32->data[i].data_type == 1 || stm32->data[i].data_type == 2)
        {
            cJSON_AddNumberToObject(item, "val", stm32->data[i].val.data_val_i);
        }
        else if (stm32->data[i].data_type == 3)
        {
            cJSON_AddNumberToObject(item, "val", stm32->data[i].val.data_val_f);
        }

        cJSON_AddItemToArray(stm32_data, item);
    }

    return stm32_data;
}

// Modbus 数据序列化
cJSON *serialize_modbus(struct Modbus *modbus)
{
    cJSON *modbus_data = cJSON_CreateArray();

    for (int i = 0; i < 6; i++)
    {
        cJSON *item = cJSON_CreateObject();
        cJSON_AddNumberToObject(item, "key", modbus->data[i].data_key);
        cJSON_AddStringToObject(item, "name", modbus->data[i].data_name);
        cJSON_AddStringToObject(item, "addr", modbus->data[i].data_addr);
        cJSON_AddNumberToObject(item, "type", modbus->data[i].data_type);

        // 根据数据类型选择序列化字段
        if (modbus->data[i].data_type == 1 || modbus->data[i].data_type == 2)
        {
            cJSON_AddNumberToObject(item, "val", modbus->data[i].val.data_val_i);
        }
        else if (modbus->data[i].data_type == 3)
        {
            cJSON_AddNumberToObject(item, "val", modbus->data[i].val.data_val_f);
        }

        cJSON_AddItemToArray(modbus_data, item);
    }

    return modbus_data;
}

// 总体序列化
char *serialize_all(const struct STM32 *stm32, const struct Modbus *modbus)
{
    cJSON *root = cJSON_CreateObject();

    cJSON_AddNumberToObject(root, "type", 10);

    // 添加 STM32 数据
    cJSON *stm32_data = serialize_stm32(stm32);
    cJSON_AddItemToObject(root, "stm32", stm32_data);

    // 添加 Modbus 数据
    cJSON *modbus_data = serialize_modbus(modbus);
    cJSON_AddItemToObject(root, "modbus", modbus_data);

    // 序列化为 JSON 字符串
    char *json_string = cJSON_Print(root);

    // 清理 cJSON 对象
    cJSON_Delete(root);

    return json_string;
}

char *serialize_up(struct history_up *his_up)
{
    cJSON *root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "type", his_up->type);
    cJSON_AddStringToObject(root,"time",his_up->time);
    cJSON_AddStringToObject(root, "mode", his_up->mode);
    cJSON_AddStringToObject(root, "data_Vol", his_up->data_Vol);
    cJSON_AddStringToObject(root, "data_Tmp", his_up->data_Tmp);
    cJSON_AddStringToObject(root, "data_Hum", his_up->data_Hum);
    char *json_string = cJSON_Print(root);
    cJSON_Delete(root);
    return json_string;
}

char *user_up(const int type)
{
    cJSON *root = cJSON_CreateObject(); 
    cJSON_AddNumberToObject(root, "type", type);
    char *json_string = cJSON_Print(root);
    cJSON_Delete(root);
    return json_string;
}